<html>
<head><meta charset="utf-8"><title>need help with types · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html">need help with types</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214535619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214535619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henil D <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214535619">(Oct 26 2020 at 05:51)</a>:</h4>
<p>This is my first time contributing to clippy. I am trying to work on: <a href="https://github.com/rust-lang/rust-clippy/issues/6209">https://github.com/rust-lang/rust-clippy/issues/6209</a></p>
<p>This is the part that I think i need to change.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">l_ty</span><span class="p">,</span><span class="w"> </span><span class="n">r_ty</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">cx</span><span class="p">.</span><span class="n">typeck_results</span><span class="p">().</span><span class="n">expr_ty</span><span class="p">(</span><span class="n">l</span><span class="p">),</span><span class="w"> </span><span class="n">cx</span><span class="p">.</span><span class="n">typeck_results</span><span class="p">().</span><span class="n">expr_ty</span><span class="p">(</span><span class="n">r</span><span class="p">));</span><span class="w"></span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="n">l_ty</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">r_ty</span><span class="p">.</span><span class="n">peel_refs</span><span class="p">().</span><span class="n">is_integral</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="n">span_lint</span><span class="p">(</span><span class="n">cx</span><span class="p">,</span><span class="w"> </span><span class="n">INTEGER_ARITHMETIC</span><span class="p">,</span><span class="w"> </span><span class="n">expr</span><span class="p">.</span><span class="n">span</span><span class="p">,</span><span class="w"> </span><span class="s">"integer arithmetic detected"</span><span class="p">);</span><span class="w"></span>
<span class="w">                    </span><span class="bp">self</span><span class="p">.</span><span class="n">expr_span</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">expr</span><span class="p">.</span><span class="n">span</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">l_ty</span><span class="p">.</span><span class="n">peel_refs</span><span class="p">().</span><span class="n">is_floating_point</span><span class="p">()</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">r_ty</span><span class="p">.</span><span class="n">peel_refs</span><span class="p">().</span><span class="n">is_floating_point</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="n">span_lint</span><span class="p">(</span><span class="n">cx</span><span class="p">,</span><span class="w"> </span><span class="n">FLOAT_ARITHMETIC</span><span class="p">,</span><span class="w"> </span><span class="n">expr</span><span class="p">.</span><span class="n">span</span><span class="p">,</span><span class="w"> </span><span class="s">"floating-point arithmetic detected"</span><span class="p">);</span><span class="w"></span>
<span class="w">                    </span><span class="bp">self</span><span class="p">.</span><span class="n">expr_span</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">expr</span><span class="p">.</span><span class="n">span</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>I am trying to figure out how to get the value of the <code>r_ty</code> if it is literal or a constant. The type of <code>l_ty</code> is <code>rustc_middle::ty::Ty</code>.</p>
<p>The <code>kind</code> method on <code>Ty</code> too don't seem to work as it just gives the kind of value it can be and not the actual value.</p>



<a name="214540576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214540576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ThibsG <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214540576">(Oct 26 2020 at 07:22)</a>:</h4>
<p>Hi <span class="user-mention" data-user-id="344239">@Henil D</span> , thanks for contributing to Clippy!<br>
Did you take a look at this documentation: <a href="https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md#retrieving-the-type-of-an-expression">https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md#retrieving-the-type-of-an-expression</a>?<br>
TL;DR I think you may need to <code>match</code> on <code>l_ty.kind()</code> to know what's the actual type kind.</p>



<a name="214541163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214541163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henil D <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214541163">(Oct 26 2020 at 07:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="342307">ThibsG</span> <a href="#narrow/stream/257328-clippy/topic/need.20help.20with.20types/near/214540576">said</a>:</p>
<blockquote>
<p>Hi <span class="user-mention silent" data-user-id="344239">Henil D</span> , thanks for contributing to Clippy!<br>
Did you take a look at this documentation: <a href="https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md#retrieving-the-type-of-an-expression">https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md#retrieving-the-type-of-an-expression</a>?<br>
TL;DR I think you may need to <code>match</code> on <code>l_ty.kind()</code> to know what's the actual type kind.</p>
</blockquote>
<p>Yes, I found that. But the problem is I want the value of the expression not just the type of it.  As I have to check if the value is either 0 or -1 or anything except it.</p>



<a name="214552104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214552104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ThibsG <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214552104">(Oct 26 2020 at 09:42)</a>:</h4>
<p>ah I see. So you have the <code>l</code> and <code>r</code> operands of the <code>Binary</code> operation (line 73).<br>
From their <code>ExprKind</code>, it seems that you can match their <code>LitKind</code> (<a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.LitKind.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.LitKind.html</a>) and get the int value behind this.</p>



<a name="214570411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214570411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henil D <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214570411">(Oct 26 2020 at 13:08)</a>:</h4>
<p>Ah got it. Thank you. Although <code>LitKind::Int(u128, LitIntType)</code> only stores a <code>u128</code> value, so i am not able to compare it with <code>-1</code>.  although it has <code>LitIntType</code> but there is no way to store negative value in there.</p>



<a name="214571109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214571109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eduardo Broto <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214571109">(Oct 26 2020 at 13:14)</a>:</h4>
<p><span class="user-mention" data-user-id="344239">@Henil D</span> if the literal is negative it will be wrapped in an unary <code>OpNeg</code> operation</p>



<a name="214571625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214571625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henil D <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214571625">(Oct 26 2020 at 13:19)</a>:</h4>
<p>Thanks <span class="user-mention" data-user-id="279272">@Eduardo Broto</span> </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">                </span><span class="k">match</span><span class="w"> </span><span class="o">&amp;</span><span class="n">r</span><span class="p">.</span><span class="n">kind</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="n">hir</span>::<span class="n">ExprKind</span>::<span class="n">Lit</span><span class="p">(</span><span class="n">lit</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="k">match</span><span class="w"> </span><span class="n">lit</span><span class="p">.</span><span class="n">node</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                            </span><span class="n">rustc_ast</span>::<span class="n">ast</span>::<span class="n">LitKind</span>::<span class="n">Int</span><span class="p">(</span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                                </span><span class="k">if</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w">  </span><span class="p">{</span><span class="w"></span>
<span class="w">                                    </span><span class="n">span_lint</span><span class="p">(</span><span class="n">cx</span><span class="p">,</span><span class="w"> </span><span class="n">INTEGER_ARITHMETIC</span><span class="p">,</span><span class="w"> </span><span class="n">expr</span><span class="p">.</span><span class="n">span</span><span class="p">,</span><span class="w"> </span><span class="s">"integer arithmetic detected"</span><span class="p">);</span><span class="w"></span>
<span class="w">                                    </span><span class="bp">self</span><span class="p">.</span><span class="n">expr_span</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">expr</span><span class="p">.</span><span class="n">span</span><span class="p">);</span><span class="w"></span>
<span class="w">                                </span><span class="p">}</span><span class="w"></span>
<span class="w">                            </span><span class="p">},</span><span class="w"></span>
<span class="w">                            </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">                        </span><span class="p">}</span><span class="w"></span>

<span class="w">                    </span><span class="p">}</span><span class="w"></span>
<span class="w">                    </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>I am not too familiar with abstract syntax trees and how they. Here <code>r</code>  is  from <a href="https://github.com/rust-lang/rust-clippy/blob/afbac8906e614a63ff5825710c3ebe45a3b5e01a/clippy_lints/src/arithmetic.rs#L73">https://github.com/rust-lang/rust-clippy/blob/afbac8906e614a63ff5825710c3ebe45a3b5e01a/clippy_lints/src/arithmetic.rs#L73</a>. And currently i am adding this inside <code>match &amp;expr.kind</code>. Is that part correct?</p>



<a name="214572119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214572119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henil D <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214572119">(Oct 26 2020 at 13:24)</a>:</h4>
<p>in plain english, I am just trying to check if <code>r</code>'s value is either 0 or not. And as you said if the value is <code>-1</code> it will be wrapped in <code>OpNeg</code>, so i will have to add a new match label on the outer match statement.</p>



<a name="214573121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214573121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eduardo Broto <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214573121">(Oct 26 2020 at 13:32)</a>:</h4>
<p>Yeah you will need probably something like that.</p>
<p>I don't have time now to check it, but from the code you linked I see that negating is already being linted, so you will have to allow it in this case too...</p>
<p>Maybe it would be a good idea to think about generalizing here, when this operation will be completely safe so we can allow it?</p>



<a name="214578321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214578321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henil D <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214578321">(Oct 26 2020 at 14:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="279272">Eduardo Broto</span> <a href="#narrow/stream/257328-clippy/topic/need.20help.20with.20types/near/214573121">said</a>:</p>
<blockquote>
<p>Yeah you will need probably something like that.</p>
<p>I don't have time now to check it, but from the code, you linked I see that negating is already being linted, so you will have to allow it in this case too...</p>
<p>Maybe it would be a good idea to think about generalizing here when this operation will be completely safe so we can allow it?</p>
</blockquote>
<p>Generalizing in the sense as you said in the open issue?</p>
<blockquote>
<p>We could special case though if the RHS of a binary operation / or % is a literal/constant that is not 0 or -1, there should be no risk of overflow/panic.</p>
</blockquote>
<p>or generalizing the code? I think I have a working code(which is quite messy), but I am not able to run <code>cargo uitest</code> as they are failing currenly... I have tried running it in the master branch too, it's failing there also so I guess it's an upstream bug as mentioned in readme.</p>



<a name="214579057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214579057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eduardo Broto <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214579057">(Oct 26 2020 at 14:19)</a>:</h4>
<p>I meant generalizing when negating is safe (can't overflow).</p>
<p>Don't hesitate to open the PR, it can be discussed further there, it's easier when you can see the code <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>
<p>About the problem running the tests, make sure you have the latest toolchain (run <code>setup-toolchain.sh</code> again) and that you're based on top of the latest master. If you still have the error a rustup may be needed as stated in the docs.</p>



<a name="214595509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/need%20help%20with%20types/near/214595509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Henil D <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/need.20help.20with.20types.html#214595509">(Oct 26 2020 at 16:13)</a>:</h4>
<p>I have opened a PR. <a href="https://github.com/rust-lang/rust-clippy/pull/6229">https://github.com/rust-lang/rust-clippy/pull/6229</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>